import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import router from './router/index'
import store from './store/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import './assets/rem/rem.ts'
import { loadRoutes } from './router/setrouter'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import zhCn from 'element-plus/es/locale/lang/zh-cn'

zhCn.el.pagination.total = '共' + `{total}` + '条数据'

// 创建并挂载应用的函数
async function bootstrap() {
    // 检查token是否存在
    const token = localStorage.getItem('token');
    if (token) {
        // 如果token存在，尝试加载路由
        const userType = localStorage.getItem('userType') || '';
        await loadRoutes(userType); // 确保等待路由加载
    }

    const app = createApp(App);

    for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        app.component(key, component)
    }

    app.use(ElementPlus, { locale: zhCn });
    app.use(router);
    app.use(store);
    app.mount('#app');
}

// 启动应用
bootstrap();
